$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'gaokaobaobei/agency/getBaobeiList',
        datatype: "json",
        colModel: [			
			{ label: '姓名', name: 'name', index: 'name', width: 60 },
			{ label: '单位', name: 'unit', index: 'unit', width: 100 },
            { label: '联系电话', name: 'phone', index: 'phone', width: 80 },
            { label: '代理人数', name: 'dailiCount', index: 'dailiCount', width: 40},
			{ label: '夏考', name: 'xiakaoCount', index: 'xiakaoCount', width: 40},
			{ label: '单招', name: 'danzhaoCount', index: 'danzhaoCount', width: 40 },
            { label: '专升本', name: 'shengbenCount', index: 'shengbenCount', width: 40 },
            { label: '考研', name: 'kaoyanCount', index: 'kaoyanCount', width: 40 },
            { label: '合作时间', name: 'cooperationtime', index: 'cooperationtime', width: 80 },
            { label: '上级代理', name: 'superior', index: 'unit', width: 60 },
			{ label: '操作',  width: 120,
			formatter:function (data,a,rows) {
				return '<button class="btn btn-default btn-sm" onclick="select(\''+rows.name+'\',\''+rows.Admissionstype+'\')">查看明细</button>' +
                       '<button class="btn btn-default btn-sm" onclick="updateDaili('+rows.userId+',\''+rows.name+'\')">修改代理</button>'

            }
			}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.getUserdTree()


   $("#jqGridZN").jqGrid({
        url: baseURL + 'gaokaobaobei/agency/getBaobeiList',
        datatype: "json",
        colModel: [
            { label: '下级代理', name: 'name', index: 'name', width: 60 },
            { label: '单位', name: 'unit', index: 'unit', width: 100 },
            { label: '联系电话', name: 'phone', index: 'phone', width: 80 },
            { label: '代理人数', name: 'dailiCount', index: 'dailiCount', width: 40},
            { label: '夏考', name: 'xiakaoCount', index: 'xiakaoCount', width: 40},
            { label: '单招', name: 'danzhaoCount', index: 'danzhaoCount', width: 40 },
            { label: '专升本', name: 'shengbenCount', index: 'shengbenCount', width: 40 },
            { label: '考研', name: 'kaoyanCount', index: 'kaoyanCount', width: 40 },
            { label: '合作时间', name: 'cooperationtime', index: 'cooperationtime', width: 80 },
            // { label: '操作',  width: 120,
            //     formatter:function (data,a,rows) {
            //         return '<button class="btn btn-default btn-sm" onclick="select(\''+rows.name+'\',\''+rows.Admissionstype+'\')">增加代理</button>'
            //     }
            // }
        ],
        viewrecords: true,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPagerZN",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGridZN").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree
var usertree

function updateDaili(id,name) {
    console.log(id)
    vm.agency.userId=id
    vm.agency.name=name
    vm.upadteDaili=true
    vm.userdeptTree()
}

function select(name,Admissionstype) {
    vm.showList = 3;
    vm.username=name
    var pageZN = $("#jqGridZN").jqGrid('getGridParam', 'page');
    $("#jqGridZN").jqGrid('setGridParam', {
        postData: {"username":name,
        },
        page: pageZN
    }).trigger("reloadGrid");
    $("#jqGridZN").setGridWidth($(window).width());
}

var vm = new Vue({
    el: '#rrapp',
    data: {
        search:null,
        showList: 0,
        showList2: true,
        showList3:false,
        title: null,
        username:null,
        agency: {},
        Admissionstype:"",
        upadteDaili:false
    },
    methods: {
        upadteDail:function(){
            console.log(vm.agency.userId)
            console.log(vm.agency.superiorId)
            $.ajax({
                type: "POST",
                url: baseURL + "gaokaobaobei/agency/updateDaili",
                contentType: "application/json",
                data: JSON.stringify(vm.agency),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.query();
                            vm.showList=0
                            vm.upadteDaili=false
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });

        },
        getNewUserdTree: function(){
            //加载新用户树
            $.get(baseURL + "gaokaobaobei/agency/getNewUserTree", function(r){
                 ztree = $.fn.zTree.init($("#newUserTree"), setting, r);
                // var node = ztree.getNodeByParam("deptId", vm.user.deptId);
                // if(node != null){
                //     ztree.selectNode(node);
                //     vm.user.deptName = node.name;
                // }
            })
        },
        getUserdTree: function(){
            //加载
            $.get(baseURL + "gaokaobaobei/agency/getUserTree", function(r){
                usertree = $.fn.zTree.init($("#userTree"), setting, r);
                // var node = ztree.getNodeByParam("deptId", vm.user.deptId);
                // if(node != null){
                //     ztree.selectNode(node);
                //     vm.user.deptName = node.name;
                // }
            })
        },
        newuserdeptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择新用户",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                     vm.agency.userId = node[0].deptId;
                     vm.agency.name = node[0].name;

                    layer.close(index);
                }
            });
        },
        userdeptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择上级代理",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#dailideptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = usertree.getSelectedNodes();
                    //选择上级部门
                    vm.agency.superiorId = node[0].deptId;
                    vm.agency.superior = node[0].name;
                    if(vm.upadteDaili){
                        vm.upadteDail()
                    }

                    layer.close(index);
                }
            });
        },

        queryZN:function(){
            var pageZN = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {"search":vm.search
                },
                page: pageZN
            }).trigger("reloadGrid");
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = 1;
            vm.title = "新增";
            vm.agency = {userId:null,superiorId:null,superior:null,name:null};
            vm.getNewUserdTree()
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = 1;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.agency.id == null ? "gaokaobaobei/agency/save" : "gaokaobaobei/agency/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.agency),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.query();
                            vm.showList=0
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "gaokaobaobei/agency/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "gaokaobaobei/agency/info/" + id, function (r) {
                vm.agency = r.agency;
            });
        },
        reload: function (event) {
            vm.showList = 0;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
})